package abel.wlmr.config;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import abel.wlmr.model.type.MeterConfigBody;
import abel.wlmr.model.type.WlmrAddrBody;

/**
 * This is a config pojo for station.
 * 
 * @author Abel
 *
 */
public class MeterStation {

    private String pwd;

    private WlmrAddrBody addr;

    private List<MeterConfigBody> meterList = new ArrayList<>();

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("{pwd:%s, addr:%s, meterList:%s}", pwd, addr, meterList);
    }

    /**
     * @return the pwd
     */
    public String getPwd() {
        return pwd;
    }

    /**
     * @param pwd
     *            the pwd to set
     */
    public void setPwd(String pwd) {
        if (pwd == null || pwd.length() != 16) {
            throw new IllegalArgumentException("pwd should be 16 ascii long");
        }

        this.pwd = pwd;
    }

    /**
     * @return the station
     */
    public WlmrAddrBody getAddr() {
        return addr;
    }

    /**
     * @param station
     *            the station to set
     */
    public void setAddr(WlmrAddrBody station) {
        if (station == null) {
            throw new IllegalArgumentException("station can't be null");
        }

        this.addr = station;
    }

    /**
     * @return the meterList
     */
    public List<MeterConfigBody> getMeterList() {
        return meterList;
    }

    /**
     * @param meterList
     *            the meterList to set
     */
    public void setMeterList(List<MeterConfigBody> meterList) {
        if (meterList == null) {
            throw new IllegalArgumentException("meter list can't be null");
        }

        Set<Short> daList = new HashSet<>();
        for (MeterConfigBody mc : meterList) {
            if (mc == null) {
                throw new IllegalArgumentException("none of meter can't be null");
            }
            if (daList.contains(mc.getMeterNo())) {
                throw new IllegalArgumentException("meter-DA can't be duplicated");
            }

            daList.add(mc.getDaNo());
        }

        this.meterList = meterList;
    }

}
